﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xaml;
using UofCASE.GreenPepe.Extensibility;
using Microsoft.VisualStudio.Modeling;
using UofCASE.AgileProductLinerDSL.Exceptions;
using System.Collections.ObjectModel;
using System.IO;

namespace UofCASE.AgileProductLinerDSL {
    /// <summary>
    /// Interaction logic for MappingAcceptanceTestSelector.xaml
    /// </summary>
    public partial class MappingAcceptanceTestSelector : Window {

        private TestShape testShape;
        
        public MappingAcceptanceTestSelector(TestShape testShape) {
            InitializeComponent();
            this.testShape = testShape;
            GreenPepeConnector connector = AgileProductLinerDSLPackage.GreenPepeConnector;
            this.treeView.ItemsSource = GetTreeData(connector.GetAllTests(testShape.Store));
            this.treeView.MouseDoubleClick += new MouseButtonEventHandler(OnMouseDoubleClick);
        }

        private void OnMouseDoubleClick(object sender, MouseButtonEventArgs e) {
            MapTest();
        }

        private void OnMapTestClick(object sender, RoutedEventArgs e) {
            MapTest();
        }

        private void MapTest() {
            MappingTreeNode node = this.treeView.SelectedItem as MappingTreeNode;

            if (node == null) {
                MessageBox.Show(VSPackage.NoTestInfoSelected);
                return;
            }
            if (node.IsFolder) {
                MessageBox.Show(VSPackage.NoTestInfoSelected);
                return;
            }

            TestInformation testInfo = node.DataContent as TestInformation;
            if (testInfo == null) {
                MessageBox.Show(VSPackage.NoTestInfoSelected);
                return;
            }
            if (testInfo.IsMapped) {
                MessageBox.Show(VSPackage.ErrTestIsAlreadyMapped);
                return;
            }

            CommandHandler.MapTest(testShape, testInfo.RelativePath, testInfo.ProjectUniqueName);
            this.Close();
        }

        private ObservableCollection<MappingTreeNode> GetTreeData(IList<TestInformation> tests) {
            ObservableCollection<MappingTreeNode> tree = new ObservableCollection<MappingTreeNode>();
            
            foreach (var test in tests) {
                
                FileInfo file = new FileInfo(test.RelativePath);
                String[] directories = test.RelativePath.Split(System.IO.Path.DirectorySeparatorChar);
                IList<String> dirs = new List<String>(directories.AsEnumerable<String>());
                dirs.Remove(file.Name);

                MappingTreeNode currentNode = null;
                ObservableCollection<MappingTreeNode> nodeList = tree;
                foreach (var item in dirs) {
                    currentNode = new MappingTreeNode(item);
                    bool currentNodeNotInNodeList = true;
                    foreach (var node in nodeList) {
                        if (currentNode.Name.Equals(node.Name)) {
                            currentNodeNotInNodeList = false;
                            currentNode = node;
                            break;
                        }
                    }
                    if (currentNodeNotInNodeList) {
                        nodeList.Add(currentNode);
                        nodeList = currentNode.Children;
                    } else {
                        nodeList = currentNode.Children;
                    }

                }
                nodeList.Add(new MappingTreeNode(test));
            }

            return tree;
        }
    }
}
